//
//  GuidePagesViewController.m
//  Yingzhi
//
//  Created by Maxwell Rose on 15/9/1.
//  Copyright (c) 2015年 yingzhi. All rights reserved.
//


#define WIDTH [[UIScreen mainScreen] bounds].size.width
#define HIGHT [[UIScreen mainScreen] bounds].size.height
#define kNumberOfGuidePages 4

#import "GuidePagesViewController.h"
#import "TabBarController.h"


@interface GuidePagesViewController ()<UIScrollViewDelegate>
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UIPageControl *pageControl;
@end

@implementation GuidePagesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HIGHT)];
    [self.view addSubview:_scrollView];

    // 1.设置scrollView的属性
    _scrollView.contentSize = CGSizeMake(WIDTH * kNumberOfGuidePages, HIGHT);
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.delegate = self;
    
    // 2.添加图片到scrollView中
    CGSize size = [[UIScreen mainScreen] bounds].size;
    NSString *imageName;
    if (size.width == 320) {
        if (size.height == 480) {//iphone 4,4s
            imageName = @"default-4-no";
        }else{//iphone 5,5s
            imageName = @"default-5-no";
        }
    }else{//iphone 6,6p
        imageName = @"default-no";
    }
    for (int i = 0; i < kNumberOfGuidePages; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i * WIDTH, 0, WIDTH, HIGHT)];
        
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@%d",imageName,i + 1]];
        imageView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
        [_scrollView addSubview:imageView];
        
        if (i == (kNumberOfGuidePages - 1)) {
            [self setupLastImageView:imageView];
        }
    }
    
    // 3.添加pageControl：分页，展示目前看的是第几页
    _pageControl = [[UIPageControl alloc]init];
    [self.view addSubview:_pageControl];
    _pageControl.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
    _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    _pageControl.numberOfPages = kNumberOfGuidePages;
    _pageControl.center = CGPointMake(WIDTH / 2, HIGHT - 20);
    
    
}

/**
 *  初始化最后一个imageView
 *
 *  @param imageView 最后一个imageView
 */

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat page=self.scrollView.contentOffset.x / self.scrollView.frame.size.width;
    self.pageControl.currentPage = page + 0.5;
}
- (void)setupLastImageView:(UIImageView *)imageView
{
    // 开启交互功能
    imageView.userInteractionEnabled = YES;

    UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    startBtn.frame = CGRectMake(0, 0, 108, 52);
    startBtn.center = CGPointMake(WIDTH * 0.5, HIGHT * 0.85);
    startBtn.layer.cornerRadius = startBtn.frame.size.height / 2;
    startBtn.layer.masksToBounds = YES;
    startBtn.layer.borderColor = [[UIColor whiteColor] CGColor];
    startBtn.layer.borderWidth = 1;
    [startBtn setTitle:@"立即启程" forState:UIControlStateNormal];
    [startBtn addTarget:self action:@selector(startClick) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:startBtn];
}
-(void)shareClick:(UIButton *)button
{
    button.selected = !button.isSelected;
}
-(void)startClick
{
    NSLog(@" %d | %s ",__LINE__,__FUNCTION__);
    TabBarController *_tabBarVC = [[TabBarController alloc]init];
    [self presentViewController:_tabBarVC animated:NO completion:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
